package com.ruoyi.web.controller.eamp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.eamp.domain.EampEmail;
import com.ruoyi.eamp.domain.vo.BingVO;
import com.ruoyi.eamp.domain.vo.EmailAndUserVO;
import com.ruoyi.eamp.service.IEampEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 邮件信息表Controller
 * 
 * @author 吴
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/eamp/email")
public class EampEmailController extends BaseController
{
    @Autowired
    private IEampEmailService eampEmailService;

    /**
     * 查询邮件信息表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:list')")
    @GetMapping("/list")
    public TableDataInfo list(EampEmail eampEmail)
    {
        startPage();
        List<EampEmail> list = eampEmailService.selectEampEmailList(eampEmail);
        return getDataTable(list);
    }

    /**
     * 导出邮件信息表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:export')")
    @Log(title = "邮件信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EampEmail eampEmail)
    {
        List<EampEmail> list = eampEmailService.selectEampEmailList(eampEmail);
        ExcelUtil<EampEmail> util = new ExcelUtil<EampEmail>(EampEmail.class);
        util.exportExcel(response, list, "邮件信息表数据");
    }

    /**
     * 获取邮件信息表详细信息
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:query')")
    @GetMapping(value = "/selectOne")
    public AjaxResult getInfo(EmailAndUserVO emailAndUserVO)
    {
        return success(eampEmailService.selectEampEmailByEmailId(emailAndUserVO));
    }

    /**
     * 新增邮件信息表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:add')")
    @Log(title = "邮件信息表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EmailAndUserVO emailAndUserVO)
    {
        emailAndUserVO.setConsigneeId(emailAndUserVO.getUserId());
        emailAndUserVO.setAddresserId(SecurityUtils.getUserId());
        emailAndUserVO.setCreateTime(new Date());
        if (emailAndUserVO.getEmailGenre() == null) {
            if (emailAndUserVO.getUeId()!=null){
                return toAjax( eampEmailService.sendEampEmailUser(emailAndUserVO));
            }else {
                eampEmailService.insertEampEmail(emailAndUserVO);
                int i =  eampEmailService.insertEampEmailUser(emailAndUserVO);
                return toAjax(i);
            }
        }else{
            if (emailAndUserVO.getUeId() == null) {
                eampEmailService.insertEampEmail(emailAndUserVO);
                int i = eampEmailService.saveEampEmailUser(emailAndUserVO);
                return toAjax(i);
            }else {
                eampEmailService.updateEampEmail(emailAndUserVO);
                return toAjax(1);
            }

        }
    }
    /**
     * 修改邮件信息表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:edit')")
    @Log(title = "邮件信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmailAndUserVO emailAndUserVO)
    {
        return toAjax(eampEmailService.updateEampEmail(emailAndUserVO));
    }
    /**
     * 删除邮件信息表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:remove')")
    @Log(title = "邮件信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{emailIds}")
    public AjaxResult remove(@PathVariable Long[] emailIds)
    {
        Long userId = SecurityUtils.getUserId();
        return toAjax(eampEmailService.deleteEampEmailByEmailIds(emailIds,userId));
    }
    /**
     * 查询邮件信息表列表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:allList')")
    @GetMapping("/allList")
    public TableDataInfo allList(EmailAndUserVO emailAndUserVO,Long delFig)
    {
        emailAndUserVO.setDelFlg(delFig);
        Long userId = SecurityUtils.getUserId();
        emailAndUserVO.setUserId(userId);
        startPage();
        List<EmailAndUserVO> list = eampEmailService.selectEmailAndUserAll(emailAndUserVO);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('eamp:email:allUserEmailList')")
    @GetMapping("/allUserEmailList")
    public TableDataInfo allUserEmailList(){
        Long userId = SecurityUtils.getUserId();
        List<SysUser> sysUsersEmail = eampEmailService.allUserEmailList(userId);
        return getDataTable(sysUsersEmail);
    }
    /**
     * 删除邮件信息表
     */
    @PreAuthorize("@ss.hasPermi('eamp:email:delete')")
    @Log(title = "邮件信息表", businessType = BusinessType.DELETE)
    @DeleteMapping("/RealDelete/{emailIds}")
    public AjaxResult delete(@PathVariable Long[] emailIds)
    {
        Long userId = SecurityUtils.getUserId();
        return toAjax(eampEmailService.deleteRealEampEmailByEmailIds(emailIds,userId));
    }
    @PreAuthorize("@ss.hasPermi('eamp:email:updateStatus')")
    @GetMapping("/updateStatus/{ueId}")
    public AjaxResult updateStatus(@PathVariable Long ueId){
        return toAjax(eampEmailService.updateStatus(ueId));
    }
    @PreAuthorize("@ss.hasPermi('eamp:email:restoreGenre')")
    @GetMapping("/restoreGenre/{ueId}")
    public AjaxResult restoreGenre(@PathVariable Long ueId){
        return toAjax(eampEmailService.restoreGenre(ueId));
    }
    @PreAuthorize("@ss.hasPermi('eamp:email:getListBing')")
    @GetMapping("/getListBing")
    public TableDataInfo getListBing(EmailAndUserVO emailAndUserVO){
        Long userId = SecurityUtils.getUserId();
        emailAndUserVO.setConsigneeId(userId);
       List<EmailAndUserVO> list= eampEmailService.getListBing(emailAndUserVO);
       List<BingVO> listBing = new ArrayList<BingVO>();
       BingVO bingVO1 = new BingVO("已读");
       BingVO bingVO2 = new BingVO("未读");
        for (EmailAndUserVO emailUserVO : list) {
            if (emailUserVO.getStatus() == 1) {
                bingVO1.setValue(bingVO1.getValue()+1);

            }else if (emailUserVO.getStatus() == 2) {
                bingVO2.setValue(bingVO2.getValue()+1);
            }
        }
        listBing.add(bingVO1);
        listBing.add(bingVO2);
        return getDataTable(listBing);
    }
}
